<?php
/**
 * 打印
 */
apf_require_controller("Abstract");

class Flow_PrintController extends AbstractController {

    public function handle_request() {
        //具有hr权限
        $role_list = $this->user_info['role_list'];
        if(!isset($role_list[Const_Flow::ROLE_HR])){
            //$this->show_tips('ERROR_AUTH',"/flow/",Const_Tip::ERROR);
        }
        $params = $this->request->get_parameters();
        if($params['do']=='staff'){//新员工信息
            $this->_print_staff($params);
            return "Flow_PrintStaff";
        }elseif($params['do']=='transfer'){//岗位薪资异动
            $this->_print_transfer($params);
            return "Flow_PrintTransfer";
        }elseif($params['do']=='dimission'){//离职办理流程
            $this->_print_dimission($params);
            return "Flow_PrintDimission";
        }
    }
    /**
     * 新员工信息打印
     */
    private function _print_staff($params){
        $entry_id = intval($params['id']);
        $flow = $this->_get_bll_flow()->get_flow_by_uid($entry_id);
        if($flow){
            if($flow['now_status'] == Const_Flow::FLOW_STATUS_DOING){
                $this->show_tips('ERROR_FLOW_NOT_COMPLETED',"/flow/",Const_Tip::ERROR);
                return;
            }
            $entry = $this->_get_bll_flow_user()->get_user_by_id($entry_id);
            //basic
            $citys = $this->_get_bll_hroa()->get_city_list(false);
            $entry['city_name'] = $citys[$entry['work_city_id']]['city_name'];
            $entry['re_entry_date'] = date('Y-m-d',strtotime($entry['re_entry_date']));
            //leader
            $leader = $this->_get_bll_hroa()->get_user_base_info($entry['parent_id']);
            $entry['parent_name'] = $leader['real_name'];
            //job
            $jobs = $this->_get_bll_hroa()->get_job_dep_info($entry['job_id']);
            $entry['position_name'] = $jobs['position_name'];
            $entry['department_name'] = $jobs['department_name'];
            //bu
            $business = $this->_get_bll_hroa()->get_business_list();
            $entry['bu_name'] = $business[$jobs['bu_id']]['bu_name'];

            $this->request->set_attribute('user',$entry);
        }else{
            $this->show_tips('ERROR_SYSTEM',"/flow/",Const_Tip::ERROR);
        }
    }

    /**
     * 岗位|薪资异动申请打印页面
     * @param $params
     * @author kyou
     */
    private function _print_transfer($params){
        if(is_numeric($params['id'])){
            $entry = $this->_get_bll_flow_transfer()->get_transfer_by_flow($params['id']);
            if($entry){
                $info = $this->_build_transfer_info($entry);
                $this->request->set_attribute('transfer',$info);
            }
        }else{
            $this->show_tips('ERROR_SYSTEM',"/flow/",Const_Tip::ERROR);
        }
    }

    /**
     * 离职打印页面
     * @param $params
     * @author kyou
     */
    private function _print_dimission($params){
        if(is_numeric($params['id'])){
            $entry = $this->_get_bll_flow_dimission()->find_by_flow_id($params['id']);
            if($entry['now_status'] != Const_Flow::FLOW_STATUS_EFFECT){
                $this->show_tips('ERROR_FLOW_NOT_COMPLETED',Flow_Urls::flow_list());
            }
            if($entry){
                $info = $this->_build_dimission_info($entry);
                $this->request->set_attribute('info',$info);
            }
        }else{
            $this->show_tips('ERROR_SYSTEM',"/flow/",Const_Tip::ERROR);
        }
    }

    /**
     * 入职bll
     * @return Bll_Flow_User
     */
    private function _get_bll_flow_user(){
        if(!$this->bll_flow_user ){
            apf_require_class('Bll_Flow_User');
            $this->bll_flow_user = new Bll_Flow_User();
        }
        return $this->bll_flow_user;
    }

    /**
     * 异动bll
     * @return Bll_Flow_Transfer
     * @author kyou
     */
    private function _get_bll_flow_transfer(){
        if(!$this->bll_flow_transfer ){
            apf_require_class('Bll_Flow_Transfer');
            $this->bll_flow_transfer = new Bll_Flow_Transfer();
        }
        return $this->bll_flow_transfer;
    }

    /**
     * 离职bll
     * @return Bll_Flow_Dimission
     * @author kyou
     */
    private function _get_bll_flow_dimission(){
        if(!$this->bll_flow_dimission ){
            apf_require_class('Bll_Flow_Dimission');
            $this->bll_flow_dimission = new Bll_Flow_Dimission();
        }
        return $this->bll_flow_dimission;
    }

    /**
     * 人员信息bll
     * @return Bll_Flow_Apply
     * @author kyou
     */
    private function _get_bll_flow_apply(){
        if(!$this->bll_flow_apply ){
            apf_require_class('Bll_Flow_ApplyRelation');
            $this->bll_flow_apply = new Bll_Flow_ApplyRelation();
        }
        return $this->bll_flow_apply;
    }
    /**
     * 待办事项
     * @return Bll_Flow_Log
     */
    private function _get_bll_flow(){
        if(!$this->bll_flow ){
            apf_require_class('Bll_Flow_Info');
            $this->bll_flow = new Bll_Flow_Info();
        }
        return $this->bll_flow;
    }
    /**
     * hroa interface bll
     * @return Bll_Hr_Property
     */
    private function _get_bll_hroa(){
        if(!$this->bll_hroa ){
            apf_require_class('Bll_Hr_OutInterface');
            $this->bll_hroa = new Bll_Hr_OutInterface();
        }
        return $this->bll_hroa;
    }
    /**
     * @return Bll_Flow_OperateLog
     */
    private function _get_bll_log(){
        if(!$this->bll_log ){
            apf_require_class('Bll_Flow_OperateLog');
            $this->bll_log = new Bll_Flow_OperateLog();
        }
        return $this->bll_log;
    }

    /**
     * 构建异动打印页面view object
     * @param $enty
     * @author kyou
     */
    private function _build_transfer_info($entry){
        //处理先前信息  格式丁林,SHF4085,上海,新房,技术部,PHP开发工程师,20120828,P5
        $beforeInfo = split(",",$entry['before_info']);
        $info['name'] = $beforeInfo[0];//姓名
        $info['code'] = $beforeInfo[1];//工号
        $info['department'] = $beforeInfo[3].$beforeInfo[4];//组装格式:新房技术部
        $info['position'] = $beforeInfo[5];//职位
        $info['enter_date'] = Util_Tools::format_date($beforeInfo[6]);//入职时间
        $info['before_pm_level'] = $beforeInfo[7];//之前PX MX

        $after_p_level = $this->_get_p_level($entry['hrd_p_level']);
        $after_m_level = $this->_get_m_level($entry['hrd_m_level']);
        if($after_p_level || $after_m_level){
            $info['after_pm_level'] = $after_p_level. " " . $after_m_level;//之后PX MX
        }else{
            $info['after_pm_level'] = $info['before_pm_level'];
        }

        $info['flow_type'] = $entry['flow_type'];//异动类型 2-岗位异动 4-薪资职级异动
        $info['plan_date'] = Util_Tools::format_date($entry['effective_date']);//建议生效日期

        $info['transfer_type'] = empty($entry['transfer_type'])?"":Const_Flow::transfer_type($entry['transfer_type']);//岗位异动类型
        $info['transfer_reason'] = $entry['transfer_reason'];
        $info['salary_reason'] = empty($entry['salary_type'])?"":Const_Flow::salary_type($entry['salary_type']);//薪资异动原因
        $info['salary_reason_remark'] = $entry['salary_reason'];//薪资异动原因备注

        //获取原主管name
        $condition['flow_stage'] = 2;
        //$condition['is_deleted'] = 1; //未删除

        $apply_relation = $this->_get_bll_flow_apply()->get_relation($entry['flow_id'] , $condition);
        $under_leader_before = $this->_get_bll_hroa()->get_user_base_info($apply_relation['parent_uid']);//获取原主管信息
        $info['under_leader_before'] = empty($under_leader_before) ? "" : $under_leader_before['user_name'];//原主管name
        //获取新主管
        $new_parent_uid = $entry['apply_user_id'];
        if(!empty($entry['new_super_uid'])){
            $new_parent_uid = $entry['new_super_uid'];
        }
        $under_leader_after = $this->_get_bll_hroa()->get_user_base_info($new_parent_uid);//获取新主管信息
        $info['under_leader_after'] = empty($under_leader_after) ? "" : $under_leader_after['user_name'];
        $superior = $this->_get_bll_hroa()->get_under_leader($new_parent_uid);//直属上级
        $info['superior_after'] = empty($superior) ? "" : $superior['name'];//异动后 上级主管

        if(empty($entry['after_info'])){//未更改职位
            $info['after_position'] = $info['position'];//异动之后职位
            $info['after_department'] = $info['department'];//异动之后部门
        }else {
            $afterInfo = split("-",$entry['after_info']);
            $info['after_position'] = $afterInfo[0];//异动之后职位
            $info['after_department'] = $afterInfo[1];//异动之后部门
        }

        if($entry['hrd_new_salary']){
            $info['hr_salary'] = $this->_get_bll_hroa()->decrypt_value($entry['hrd_new_salary']);//薪资核定
        }elseif ($entry['hr_new_salary']){
            $info['hr_salary'] = $this->_get_bll_hroa()->decrypt_value($entry['hr_new_salary']);//薪资核定
        }else{
            $info['hr_salary'] = "--";
        }

        //获取hr_name
        $hr_names = $this->_get_hr_names($entry['flow_id']);
        $info['hr_name'] = $hr_names['hr_name'];
        $info['hrd_name'] = $hr_names['hrd_name'];

        return $info;
    }
    /**
     * 构建离职打印页面view object
     * @param $enty
     * @author kyou
     */
    private function _build_dimission_info($entry){
        $userInfo = $this->_get_bll_hroa()->get_user_base_info($entry['user_id'] , 0);//获取员工信息 职位信息
        $dep_info = $this->_get_bll_hroa()->get_job_dep_info($entry['job_id']);
        $info['name'] = $userInfo['user_name'];
        $info['department'] = $dep_info['department_name'];
        $info['position'] = $dep_info['position_name'];
        $info['enter_date'] = $userInfo['enter_date'];//入职时间
        $info['last_date'] = $entry['last_date'];//最后工作时间

        $info['handoff_desc'] = $entry['handoff_desc'];//部门交接资料
        $info['handoff_user_name'] = empty($entry['handoff_user_name'])? "无":$entry['handoff_user_name'] ;//工作交接人姓名
        $info['handoff_re_desc'] = $entry['handoff_re_desc'];//直属上级交接信息
        $info['superior_re_opinion'] = empty($entry['superior_re_opinion'])? "未完成" : "完成";//直属上级是否交接完成

        //未结清款项数据
        $info['uncleared_type'] = $entry['uncleared_type']==1 ? "有":"无";
        $info['uncleared_sale_type'] = $entry['uncleared_sale_type']==1 ? "有":"无";

        if($entry['uncleared_type'] ==1){
            $info['uncleared_text'] = $this->_get_fin($entry['uncleared_text']);
            $info['uncleared_money']= $entry['uncleared_money']=='0.00' ? '' : $entry['uncleared_money'];
        }
        if($entry['uncleared_sale_type'] ==1){
            $info['uncleared_sale_text'] = $this->_get_fin($entry['uncleared_sale_text']);
            $info['uncleared_sale_money']= $entry['uncleared_sale_money']=='0.00' ? '' : $entry['uncleared_sale_money'];
        }

        $info['uncleared_desc'] = $entry['uncleared_desc'];//费用说明
        $info['uncleared_bak'] = $entry['uncleared_bak'];//财务备注
        $is_tech = $this->_get_bll_hroa()->check_is_tech($userInfo['department_id']);
        $info['is_tech'] = $is_tech;
        //技术部
        $info['book_back'] = $entry['book_back'] == 1 ? "已归还" : "未归还" ;//书籍归还 1已归还 2未归还
        $info['book_bak'] = $entry['book_bak'] ;//书籍归还备注
        //IT部
        $info['close_power'] = $entry['close_power'] == 1 ? "已关闭" : "未关闭" ;//关闭权限 1已关闭 2未关闭
        $info['close_bak'] = $entry['close_bak'] ;//备注
        //行政部
        $info['callback_equipment'] = $this->_get_equipment_by_code($entry['callback_type']);
        $info['callback_desc'] = $entry['callback_desc'] ;//备注
        //人力资源部
        $info['option'] = $entry['option'] == 1 ? "有" : "没有";//是否有期权 1有 2没有
        $info['work_book'] = $entry['work_book'] == 1 ? "已归还" : "未归还";//是否归还工作手册 1有 0无
        $info['hr_checked'] = $this->_get_hr_checked_by_code($entry['hr_checked']);//已办理 1退工单离职证明 2退保险 3退公积金 4修改员工通信录
        $info['hr_bak'] = $entry['hr_bak'] ;//备注

        //get operator user list
        $info['user_list'] = $this->_get_bll_log()->get_flow_user_log($entry['flow_id']);
        return $info;
    }


    private function _get_fin($string){
        $res = '';
        if(empty($string)){
            return $res;
        }
        $res_txt = array();
        $finan_list = Const_Flow::finan_list();
        $code_list = explode(',', $string);
        foreach($code_list as $val){
            $res_txt[]= $finan_list[$val];
        }
        $res = implode(' , ', $res_txt);
        return $res;
    }
    /**
     * 收回设备
     * 说明:1员工证 2门禁卡 3办公用具 4名片 5钥匙 6电脑 7办公电话端口 用逗号分割
     * @param string $code
     * @author kyou
     */
    private function _get_equipment_by_code($codeString){
        $res = " ";
        if(empty($codeString)){
            return $res;
        }
        $res_txt = array();
        $office_supply = Const_Flow::office_supply();
        $code_list = explode(',', $codeString);
        foreach ($code_list as $val){
            $res_txt[] = $office_supply[$val];
        }
        $res = implode(' , ',$res_txt);
        return $res;
    }
    /**
     * 已办理
     * 说明:1退工单离职证明 2退保险 3退公积金 4修改员工通信录
     * @param string $code
     * @author kyou
     */
    private function _get_hr_checked_by_code($codeString){
        $res = " ";
        if(empty($codeString)){
            return $res;
        }
        $res_txt = array();
        $hr_confirm = Const_Flow::hr_confirm();
        $code_list = explode(',', $codeString);
        foreach ($code_list as $val){
            $res_txt[] = $hr_confirm[$val];
        }
        $res = implode(' , ',$res_txt);
        return $res;
    }

    /**
     * 获取P级线
     * @param int $level
     */
    private function _get_p_level($level){
        $p_degree_list = $this->_get_bll_hroa()->get_p_degree();
        $p_degree = $p_degree_list[$level];
        return $p_degree['degree'];
    }
    /**
     * 获取M级线
     * @param int $level
     */
    private function _get_m_level($level){
        $m_degree_list = $this->_get_bll_hroa()->get_m_degree();
        $m_degree = $m_degree_list[$level];
        return $m_degree['degree'];
    }
    /**
     * 获取hr名和hrd名
     * @param
     */
    private function _get_hr_names($flow_id){
        $hr_name = $hrd_name ="";
        $bll_log = $this->_get_bll_log();
        $hr_log = $bll_log->get_log_by_id_role($flow_id, Const_Flow::ROLE_HR, Const_Flow::PHASE_18);//hr log
        if (!empty($hr_log)){
            $hr_info = $this->_get_bll_hroa()->get_user_base_info($hr_log['opt_user_id']);
            if(!empty($hr_info)){
                $hr_name = $hr_info['user_name'];
            }
        }

        $hrd_log = $bll_log->get_log_by_id_role($flow_id, Const_Flow::ROLE_HRD);//hrd log
        if (!empty($hrd_log)){
            $hrd_info = $this->_get_bll_hroa()->get_user_base_info($hrd_log['opt_user_id']);
            if(!empty($hrd_info)){
                $hrd_name = $hrd_info['user_name'];
            }
        }
        return array("hr_name"=>$hr_name,"hrd_name"=>$hrd_name);
    }
}